package com.fenghongzhang.day005;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class LeftFragment extends Fragment {
    private EditText leftEdit;
    private Button leftBtn;


    private MyListener myListener;

    public interface MyListener{
        void sendMessage(String s);
    }

    public LeftFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        myListener = (MyListener) getFragmentManager().findFragmentById(R.id.right);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_left, container, false);

        leftEdit = (EditText) inflate.findViewById(R.id.left_edit);
        leftBtn = (Button) inflate.findViewById(R.id.left_btn);

        leftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = leftEdit.getText().toString();

                //1,静态,通知byId 来找.
//                TextView viewById = getFragmentManager().findFragmentById(R.id.right).getView().findViewById(R.id.right_text);
//                viewById.setText(s);
                //2,动态通过bytag来找.

                //getFragmentManager().findFragmentByTag("right").getView().findViewById(R.id.right_text);
                //3
//                TextView viewById1 = getActivity().findViewById(R.id.right_text);
//                viewById1.setText(s);

                //接口回调的方式
                myListener.sendMessage(s);

            }
        });

        return inflate;
    }
}
